﻿Imports System.Transactions
Public Class CopyTask
    Public Sub Copy(ByVal _jobListRow As Job.JobListRow)
        Dim sqlall As String = ""
        Dim sqlbd As String = ""
        Dim sqlrev As String = ""
        Dim count As Integer = 0
        Dim batchadap As New FundCenter2TableAdapters.FCReleasesTableAdapter
        Dim dd As New DataTable
        Dim Myversion As Integer
        Dim username As String = _jobListRow("SubmitBy")
        Dim planYear As String = _jobListRow("PlanYear")
        Dim top_fct As String
        Dim version As Integer
        Dim copyBatch As New CopyBatch()
        copyBatch.GetTopFct(username, planYear, 1, top_fct, version)

        Dim roleType As String = ""
        Dim adapUsers As New UserTableAdapters.UsersTableAdapter
        Dim dtUsers As New User.UsersDataTable
        dtUsers = adapUsers.GetDataUser(username)
        If dtUsers.Rows.Count() > 0 Then
            roleType = dtUsers(0)("RoleType")
        End If
        If Val(roleType) = 5 Or Val(roleType) = 1 Then
            top_fct = "0000"
        End If

        Dim FCTDataSet As New DataSet
        copyBatch.GetChildWithVersion(top_fct, planYear, 1, FCTDataSet, Myversion)

        dd = FCTDataSet.Tables("Items")
        For Each row As DataRow In dd.Rows
            Try
                If row.Item("ReleaseFlag") = "N" And Val(row.Item("level")) < 4 Then
                    count = count + 1
                    Exit For
                End If
                If row.Item("ReleaseFlag") = "" Then
                    count = count + 1
                    Exit For
                End If
            Catch ex As Exception
                count = count + 1
                Exit For
            End Try
        Next
        If count > 0 Then
            Throw New Exception("มีหน่วยงาน ยังไม่ได้ส่งแผน ไม่สามารถ Copy Version ได้ในขณะนี้")
            Exit Sub
        End If

        Dim adapter As New FundCenter2TableAdapters.BudgetDetailsTableAdapter
        Dim dtrow As New FundCenter2.BudgetDetailsDataTable
        Dim adapAt As New FundCenter2TableAdapters.AttachmentsTableAdapter
        Dim adapRev As New FundCenter2TableAdapters.BudgetRevPlansTableAdapter
        Dim dtRev As New FundCenter2.BudgetRevPlansDataTable
        Dim adapBud12 As New FundCenter2TableAdapters.Budget12TableAdapter
        Dim Badapter As New FundCenterRelationTableAdapters.BudgetDetailsTableAdapter
        Dim newVersion As Integer = _jobListRow("ToVersion")
        Dim topcount As Integer = 0
        Dim adapRel As New FundCenter2TableAdapters.FundCenterReleasesTableAdapter
        Dim rfadap As New FundCenter2TableAdapters.FundCenterReleasesTableAdapter

        topcount = Val(adapRel.CheckFundRelease(dd.Rows(0).Item("FundCenterCode"), Val(planYear), newVersion))
        If topcount > 0 Then
            Throw New Exception("ระบบไม่สามารถ Copy Version ที่ทำการ Copy Version มาแล้วได้ หากต้องการ Copy Version อีกครั้งให้ทำการ Release Version ก่อน จากนั้นทำการ Copy Version อีกครั้ง")
            Exit Sub
        End If

        Using scope As New TransactionScope()
            Try
                For Each row As DataRow In dd.Rows
                    If Val(row.Item("level")) >= 4 Then
                        Dim chBudg As Integer = 0
                        chBudg = rfadap.CheckFundRelease(row.Item("FundCenterCode"), planYear, "1")
                        If chBudg = 0 Then
                            sqlall += copyBatch.BatchInsert(planYear, "N", "Y", "1", 0, Date.Now, "BATCH", row.Item("FundCenterCode"), planYear)
                        End If
                    End If
                Next

                Try
                    batchadap.Execute(sqlall)
                Catch ex As Exception
                End Try

                sqlall = ""
                For Each row As DataRow In dd.Rows ' ,set FundCenterRelease
                    Dim chBudg As Integer = 0
                    chBudg = rfadap.CheckFundRelease(row.Item("FundCenterCode"), planYear, newVersion)
                    If chBudg = 0 Then
                        dtrow = adapter.GetBudget_id(row.Item("FundCenterCode"), planYear, row.Item("Now_version"))
                        If dtrow.Rows.Count = 0 Then
                            'dtrow = adapter.GetBudget_id2(row.Item("FundCenterCode"), planYear)' Comment By Orn 29042558 เพราะ copy ข้อมูลมาไม่ครบ
                            dtrow = adapter.GetBudget_id3(row.Item("FundCenterCode"), planYear)
                        End If
                        For Each rowId As FundCenter2.BudgetDetailsRow In dtrow    'copy budget,copy attatment
                            Dim newId As Integer = adapter.copyVersion(newVersion, rowId.Id)
                            sqlbd += copyBatch.BatchcopyAtt(newId, rowId.Id)
                            sqlbd += copyBatch.BatchcopyB12(newVersion, newId, rowId.Id)
                        Next
                        Try
                            batchadap.Execute(sqlbd)
                        Catch ex As Exception
                        End Try

                        sqlbd = ""
                        dtRev = adapRev.GetDataBy_Func(row.Item("FundCenterCode"), planYear, row.Item("Now_version"))
                        If dtRev.Rows.Count = 0 Then
                            dtRev = adapRev.GetDataBy_Func2(row.Item("FundCenterCode"), planYear)
                        End If
                        For Each rowIdR As FundCenter2.BudgetRevPlansRow In dtRev    'copy rev_plan
                            sqlrev += copyBatch.BatchCopyRev(newVersion, rowIdR.Id)
                        Next
                        Try
                            batchadap.Execute(sqlrev)
                        Catch ex As Exception
                        End Try

                        sqlrev = ""
                        Dim adap As New checkPlanDSTableAdapters.FundCenterReleasesTableAdapter
                        Dim preVer As String = row.Item("Now_version")
                        'Edit By Orn 18/08/2558
                        'sqlall += copyBatch.BatchOldRelease("N", Date.Now, "BATCH", row.Item("FundCenterCode"), planYear, row.Item("Now_version")) 
                        'sqlall += copyBatch.BatchInsert(planYear, "N", "Y", newVersion, preVer, Date.Now, "BATCH", row.Item("FundCenterCode"), planYear)

                        'เปลี่ยน Flag lastversion ของ version เก่า
                        sqlall += copyBatch.BatchOldReleaseCopy("N", Date.Now, "BATCH", row.Item("FundCenterCode"), planYear)
                        'insert version ใหม่
                        sqlall += copyBatch.BatchInsert(planYear, "N", "Y", newVersion, newVersion - 1, Date.Now, "BATCH", row.Item("FundCenterCode"), planYear)
                    End If
                Next
                Try
                    batchadap.Execute(sqlall)
                Catch ex As Exception
                End Try

                sqlall = ""
                scope.Complete()
            Catch ex As Exception
                Transaction.Current.Rollback()
                Throw New Exception(ex.Message)
            End Try
        End Using
    End Sub
End Class
